package com.gongpingjialoan.adapter.car;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.cc.Const;
import com.gongpingjialoan.data.NewCarBrand;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.imageload.ImageLoad;
import com.gongpingjialoan.utility.BaseViewHolder;

import java.util.List;

import static com.alibaba.sdk.android.feedback.impl.FeedbackAPI.mContext;

/**
 * Created by Administrator on 2015/9/29.
 */
public class BrandAdapter extends BaseAdapter {

    private List<NewCarBrand> newCarBrand;
    private Context context;
    private int position = -1;


    public BrandAdapter(List<NewCarBrand> newCarBrand, Context context) {
        this.newCarBrand = newCarBrand;
        this.context = context;
    }

    public void setPosition(int i) {
        position = i;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return newCarBrand.size();
    }

    @Override
    public Object getItem(int i) {
        return newCarBrand.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.newcar_brand_item, null, false);
        }
        TextView tv = BaseViewHolder.get(convertView, R.id.title);
        ImageView brand_img = BaseViewHolder.get(convertView, R.id.brand_img);
        tv.setText(newCarBrand.get(i).getBrand());
        if (i != position) {
            tv.setTextColor(context.getResources().getColor(R.color.discount_title));
        } else {
            tv.setTextColor(context.getResources().getColor(R.color.nav_bg_color));
        }
        if("全部品牌".equals(newCarBrand.get(i).getBrand())){
            brand_img.setVisibility(View.GONE);
        }else{
            String filename = GPJApplication.getInstance().getCategoryData().getLogoByBrand(newCarBrand.get(i).getBrand_slug());
            String logo_url =
                    Const.IMG_DOMAIN
                            + GPJApplication.getInstance().getApiUrlFromMeta("brand_model_logo_img")
                            + filename + "?imageView2/0/w/100/h/100";
            brand_img.setVisibility(View.VISIBLE);
            ImageLoad.LoadImage(brand_img, logo_url, R.drawable.brandnull, R.drawable.brandnull, mContext);
        }

        return convertView;
    }
}
